﻿using EMoney.IStock.Client.DataAccess.Stat;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models.Stat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace EMoney.IStock.Client.Business.Stat
{
    /// <summary>
    /// 用户登录基础信息业务处理类
    /// </summary>
    public class UserExtendInfoBll
    {
        private static object lockUserExtendInfo = new object();
        private static DateTime lastWriteToDatabase = DateTime.Now;
        private static List<UserExtendInfo> ListUserExtendInfo = new List<UserExtendInfo>();

        /// <summary>
        /// 新增用户登录基础信息
        /// </summary>
        /// <param name="reqUserID"></param>
        /// <param name="reqGid"></param>
        /// <param name="encrypt"></param>
        public void AddUserExtendInfo(string reqUserID, string reqGid, string reqEncrypt)
        {
            int userid = 0;

            if (!int.TryParse(reqUserID, out userid))
            {
                return;
            }

            ListUserExtendInfo.Add(new UserExtendInfo() {
                encrypt = reqEncrypt,
                gid = reqGid,
                updatetime = DateTime.Now,
                userid = userid
            });
            
            try
            {
                if (DateTime.Now.Subtract(lastWriteToDatabase).TotalMinutes >= ConfigHelper.UserExtendInfoWriteMinutes)
                {
                    lock (lockUserExtendInfo)
                    {
                        if (DateTime.Now.Subtract(lastWriteToDatabase).TotalMinutes >= ConfigHelper.UserExtendInfoWriteMinutes)
                        {
                            lastWriteToDatabase = DateTime.Now;
                            List<UserExtendInfo> newList = Interlocked.Exchange(ref ListUserExtendInfo, new List<UserExtendInfo>());

                            if (newList != null && newList.Count > 0)
                            {
                                ThreadUserExtendInfo thread = new ThreadUserExtendInfo(newList);
                                ThreadPool.QueueUserWorkItem(new WaitCallback(thread.ThreadProc), thread);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(ex, "新增用户登录基础信息失败");
            }            
        }
    }

    public class ThreadUserExtendInfo
    {
        private List<UserExtendInfo> ListUserExtendInfo = new List<UserExtendInfo>();

        public ThreadUserExtendInfo(List<UserExtendInfo> list)
        {
            ListUserExtendInfo = list;
        }

        /// <summary>
        /// 使用线程新增用户登录基础信息
        /// </summary>
        /// <param name="sender"></param>
        public void ThreadProc(object sender)
        {
            if (ListUserExtendInfo != null && ListUserExtendInfo.Count > 0)
            {
                string toEncrypt = string.Empty;
                UserExtendInfoDal dal = new UserExtendInfoDal();

                foreach (UserExtendInfo info in ListUserExtendInfo)
                {
                    try
                    {
                        //1.MD5验证
                        toEncrypt = string.Concat(info.userid, info.gid, ConfigHelper.UserExtendInfoKey);
                        if (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(toEncrypt, "md5").Equals(info.encrypt, StringComparison.OrdinalIgnoreCase))
                        {
                            //2.写数据
                            dal.AddUserExtendInfo(info);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.DefaultLogger.Error(ex, "使用线程新增用户登录基础信息失败");
                    }
                }
            }
        }
    }
}
